using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace MonoCash.ObjectModel
{
    /// <summary>
    /// Replaces ID columns with ComboBox controls populated with data.
    /// </summary>
    public partial class DataGridView : UserControl
    {
        public DataGridView()
        {
            InitializeComponent();
        }

        private object dataSource;
        public object DataSource
        {
            get { return dataSource; }
            set
            {
                dataSource = value;
                try
                {
                    SetGrid();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error setting datasource.\n" + ex.Message);
                }
            }
        }

        private void SetGrid()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = DataSource;
            dgvTable.DataSource = bs;
            dgvTable.DataError += new DataGridViewDataErrorEventHandler(dgvTable_DataError);

            List<DataGridViewComboBoxColumn> newColumns = new List<DataGridViewComboBoxColumn>();
            foreach (DataGridViewColumn col in dgvTable.Columns)
            {
                if (col.ValueType.Namespace == "MonoCash.ObjectModel" || col.DataPropertyName == "DefaultDisplayValue")
                {
                    col.Visible = false;
                }
                if (col.DataPropertyName.EndsWith("ID") && !col.DataPropertyName.Contains("Transaction"))
                {
                    col.Visible = false;
#if DEBUG
                    col.Visible = true;
#endif
                    if (col.DataPropertyName.Length > 2)
                    {
                        DataGridViewComboBoxColumn newCol = new DataGridViewComboBoxColumn();

                        string objectName = col.DataPropertyName;
                        objectName = objectName.Substring(0, objectName.Length - 2); //removes ID
                        
                        newCol.HeaderText = objectName;
                        objectName = objectName.Replace("Parent", "").Replace("Offset","");
                        ObjectModel thisObjectType = (ObjectModel)Activator.CreateInstance(Type.GetType("MonoCash.ObjectModel." + objectName));

                        newCol.ValueType = Type.GetType("System.Int32");
                        newCol.DataPropertyName = col.DataPropertyName;
                        col.DataPropertyName = "";
                        newCol.HeaderText = objectName;
                        newCol.DisplayIndex = col.DisplayIndex;
                        newCol.DisplayMember = "DefaultDisplayValue";
                        newCol.ValueMember = "ID";
                        BindingSource thisBs = new BindingSource();
                        thisBs.DataSource = thisObjectType.Objects;
                        newCol.DataSource = thisBs;

                        newColumns.Add(newCol);
                    }
                }

            }
            foreach (DataGridViewComboBoxColumn col in newColumns)
            {
                dgvTable.Columns.Add(col);
            }

        }
        void dgvTable_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = true;
            //MessageBox.Show("Error updating data.\n" + e.Exception.Message);
        }
    }
}
